package cn.swenty.backstage.framework.taglib;

import java.util.Map;

import cn.swenty.backstage.framework.common.util.StringUtil;
import cn.swenty.backstage.framework.database.annotation.TableSeg;
import cn.swenty.backstage.framework.database.entity.Entity;
import cn.swenty.backstage.framework.util.JsonUtils;

public class EntityFieldTextTag extends SimpleTag{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String entity;
	
	private String filter;
	
	private String field;
	
	
	

	public String getEntity() {
		return entity;
	}




	public void setEntity(String entity) {
		this.entity = entity;
	}




	public String getFilter() {
		return filter;
	}




	public void setFilter(String filter) {
		this.filter = filter;
	}




	public String getField() {
		return field;
	}




	public void setField(String field) {
		this.field = field;
	}




	@Override
	protected String getPrintStr() throws Exception {
		Class c= Class.forName(entity);
		TableSeg ts = (TableSeg) c.getAnnotation(TableSeg.class);
		Object o =  c.newInstance();
		if(o instanceof Entity && ts != null){
			Entity e = (Entity)o;
			if(StringUtil.isNotNull(filter)){
				Map<String, Object>  map = JsonUtils.parseJSONMap(filter);
				e.putAll(map);
			}
			 e = e.queryOne();
			 if(e != null){
				 return e.get(field).toString();
			 }
		}
		return null;
	}
}
